package org.example.User;

import org.example.JDBC.*;
import org.jetbrains.annotations.NotNull;
import sun.awt.image.ImageWatched;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class UserDao implements Insert, Select, Fill, Delete {
    /**
     *  将一个user对象插入到数据库中
     * @param o
     * @throws SQLException
     */
    @Override
    public void insert(@NotNull Object o){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement("insert into user value(null,?,?);");
            //将user的信息放进preparedStatement
            User user=(User)o;
            preparedStatement.setString(1,user.getUserName());
            preparedStatement.setString(2,user.getUserPassword());
            System.out.println(preparedStatement);
            //将数据插入数据库
            int count = preparedStatement.executeUpdate();
            if(count!=1){
                System.out.println("插入失败");
            }
            else{
                System.out.println("插入成功");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.Clear(connection, preparedStatement,null);
        }
    }

    /**
     * 通过ID查找用户
     * @param id
     * @return
     * @throws SQLException
     */
    public User selectById(int id){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        User resUser;
        try {
            connection=DBUtil.getConnection();
            preparedStatement=connection.prepareStatement("select * from user where user_id=?;");
            preparedStatement.setInt(1,id);
            resultSet = preparedStatement.executeQuery();
            resUser = new User();
            if(resultSet.next()){
                fill(resUser,resultSet);
            }
            return resUser;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.Clear(connection,preparedStatement,resultSet);
        }
    }

    /**
     *  通过名称查找用户
     * @param name
     * @return
     * @throws SQLException
     */
    public User selectByName(String name){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet = null;
        User resUser;
        try {
            connection=DBUtil.getConnection();
            preparedStatement=connection.prepareStatement("select * from user where user_name<=>?");
            preparedStatement.setString(1,name);
            resultSet = preparedStatement.executeQuery();
            resUser = new User();
            if(resultSet.next()){
                fill(resUser,resultSet);
            }
            return resUser;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.Clear(connection,preparedStatement,resultSet);
        }
    }
//+---------------+-------------+------+-----+---------+----------------+
//        | Field         | Type        | Null | Key | Default | Extra          |
//            +---------------+-------------+------+-----+---------+----------------+
//            | user_id       | int(11)     | NO   | PRI | NULL    | auto_increment |
//            | user_name     | varchar(50) | YES  |     | NULL    |                |
//            | user_password | varchar(50) | YES  |     | NULL    |                |
//            +---------------+-------------+------+-----+---------+----------------+

    /**
     * 得到所有的用户
     * @return
     * @throws SQLException
     */
    public List<User> selectAll(){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            List<User>list=new LinkedList<>();
            connection = DBUtil.getConnection();
            preparedStatement=connection.prepareStatement("select * from user;");
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                //创建一个user对象，并且填充内容
                User user=new User();
                fill(user,resultSet);
                list.add(user);
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.Clear(connection,preparedStatement,resultSet);
        }
    }


    @Override
    public <T> void fill(T t, ResultSet resultSet) throws SQLException {
        User user=(User)t;
        try {
            user.setUserId(resultSet.getInt("user_id"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            user.setUserName(resultSet.getString("user_name"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            user.setUserPassword(resultSet.getString("user_password"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param o
     */
    @Override
    public void delete(Object o){
        User user=(User)o;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            connection=DBUtil.getConnection();
            preparedStatement = connection.prepareStatement("delete from user where id<=>?;");
            preparedStatement.setInt(1,user.getUserId());
            int count = preparedStatement.executeUpdate();
            if(count!=1){
                System.out.println("插入成功");
            }
            else{
                System.out.println("插入失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.Clear(connection,preparedStatement,null);
        }
    }
}
